import logging
from service.device.device_actor import DeviceActor


deviceActorMap = {}

def changeRecv(deviceName:str,recv:int):
    if deviceName in deviceActorMap:
        logging.info("修改设备recv %s  %s" %(deviceName,recv))
        deviceActor:DeviceActor = deviceActorMap[deviceName]
        deviceActor.recv = recv


def addDeviceActor(deviceName:str):
    if deviceName not in deviceActorMap:
        logging.info("添加设备 %s" %(deviceName,))
        deviceActor = DeviceActor(deviceName)
        deviceActorMap[deviceName] = deviceActor
        deviceActor.connect()

def unlineDeviceActor(deviceName:str):
    if deviceName in deviceActorMap:
        logging.info("设备掉线 %s" %(deviceName,) )
        deviceActor:DeviceActor = deviceActorMap[deviceName]
        deviceActor.disconnect()
#        del deviceActorMap[deviceName]

def listDeviceActor():
    return list(deviceActorMap.values())

def getDeviceActorMap():
    return deviceActorMap


def release():
    for deviceName in deviceActorMap:
        logging.info("关闭设备 %s" %(deviceName,) )
        deviceActor:DeviceActor = deviceActorMap[deviceName]
        deviceActor.disconnect()
        deviceActor.delete()
    deviceActorMap.clear()



